package cn.itsource.system.controller;

import cn.itsource.system.domain.Role;
import cn.itsource.system.query.RoleQuery;
import cn.itsource.system.service.IRoleService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：员工Role
 */
@RestController
@RequestMapping(value = "/role")
@Api(tags = "员工接口类")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Role> findAll(){
        return roleService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Role findOne(@PathVariable("id")Long id){
        return roleService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            roleService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param role
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Role role){
        try {
           if(role.getId()==null){
               roleService.add(role);
           }else{
               roleService.update(role);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            roleService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param roleQuery
     * @return
     */
    @PostMapping
    public PageInfo<Role> queryPage(@RequestBody RoleQuery roleQuery){
        return roleService.queryPage(roleQuery);
    }
}
